import BaseController from '../../core/controllers/Base.controller';
import storageApi from '../../core/storage/storage';

@Inject('$log', '$rootScope', 'staffService', 'ngDialog', 'ngDialogService')
class WorkerRegistrationController extends BaseController {
  constructor() {
    super(staffService, 'queryRegistration');

    this.$setQueryCallback((res) => {
      this.workerCollectList = res.rows;
    });

    this.mapParams = {
      orgId: $rootScope.orgId || storageApi.get('orgId'),
      fullName: '',
      idNo: '',
    };

    this.$paginationQuery();
    this.toggleAllFlag = false;
  }

  $reset() {
    this.mapParams.fullName = '';
    this.mapParams.idNo = '';
  }

  /*eslint-disable */
  $toggleAll() {
    this.workerCollectList.forEach((item) => {
      item.checked = this.toggleAllFlag;
    });
  }

  // 判断全选人
  $isChange() {
    let bool = true;
    this.workerCollectList.forEach((item) => {
      if (!item.checked) {
        bool = false;
      }
    });
    this.toggleAllFlag = bool;
  }

  // 获取选中人员信息
  $getSelectedWorkers() {
    let selectedWorkerList = [];
    let length = this.workerCollectList.length;
    for (let i = 0; i < length; i++) {
      let item = this.workerCollectList[i];
      if (item.checked) {
        selectedWorkerList.push(item);
      }
    }
    return selectedWorkerList;
  }

  // 删除
  $delete() {
    const ids = [];
    let selectedWorkerList = this.$getSelectedWorkers();
    if (!selectedWorkerList.length) {
      this.ngDialogService.alertError('请选择人员');
      return;
    };
    selectedWorkerList.forEach((item) => {
      if (item.checked) {
        ids.push(item.id);
      }
    });
    this.ngDialogService.confirm('确认要删除这些人员信息吗？', ids)
      .then((data) => {
        this.staffService
          .deleteRegistration( ids )
          .then((res) => {
            if (res.success) {
              this.$paginationQuery();
              this.ngDialogService.alertSuccess(res.message);
            } else {
              this.ngDialogService.alertError(res.message);
            }
          });
      });
  }

  // 刷新（局部还是全局？）
  $refresh() {
    this.$paginationQuery();
  }

}

export default WorkerRegistrationController;
